package com.gp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gp.dao.UserDao;
import com.gp.entity.User;
import com.gp.service.UserService;
import com.gp.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 来一杯凉咖
 * @create 2022/3/9 10:22
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    /**
     * 查询所有用户
     * @return
     */
    @Override
    public List<User> findAllUser() {
        QueryWrapper<User> wrapper =new QueryWrapper<>();
        return userDao.selectList(wrapper);
    }

    /**
     *用户分页
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<User> getAllUserForPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<User> allUserList = userDao.queryAll();
        PageInfo<User> pageInfo = new PageInfo<>(allUserList);
        return pageInfo;
    }

    /**
     * 添加用户
     * 修改用户
     * @param user
     * @return
     */
    @Override
    public CommonResult editUser(User user) {
        int i;
        if (user.getUserId() == null) {
            i = userDao.insert(user);
        } else {
            i = userDao.updateById(user);
        }
        if (i > 0) {
            return new CommonResult(200, "操作成功", null);
        } else {
            return new CommonResult(500, "操作失败", null);
        }
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @Override
    public CommonResult deleteUserById(Integer userId) {
        int i = userDao.deleteById(userId);
        if (i > 0) {
            return new CommonResult(200, "删除成功", null);
        } else {
            return new CommonResult(500, "删除失败", null);
        }
    }

//    @Override
//    public CommonResult addRoleForUserByUserId(Integer userId) {
//        int i = userDao.addRoleById(userId);
//        if (i > 0) {
//            return new CommonResult(200, "添加成功", null);
//        } else {
//            return new CommonResult(500, "添加失败", null);
//        }
//    }

    /**
     *  模糊查询用户
     * @param page
     * @param size
     * @param uname
     * @param lname
     * @param email
     * @param phone
     * @return
     */
    @Override
    public PageInfo<User> queryUser(Integer page,Integer size,String uname,String lname,String email,String phone) {
        PageHelper.startPage(page,size);
        List<User> UserList = userDao.selectUser(uname,lname,email, phone);
        PageInfo<User> pageInfo = new PageInfo<>(UserList);
        return pageInfo;
    }

}
